//
//  CalculatorButtonItem.swift
//  ToDo
//
//  Created by yanhangpu on 2021/7/7.
//

import Foundation
import UIKit

/*
 let calculatorData:Array<Any> = [
                                     ["AC","+/-","%","÷"],
                                     ["7","8","9","×"],
                                     ["4","5","6","+"],
                                     ["1","2","3","-"],
                                     ["0",".","="]
                                 ]
 */
enum CalculatorButtonItem {
    
    enum Operation: String {
        case plus = "+"
        case minus = "-"
        case divide = "÷"
        case multiply = "×"
        case equal = "="
    }
    
    enum Command: String {
        case clear = "AC"
        case flip = "+/-"
        case percent = "%"
    }
    /// 0-9
    case digit(Int)
    /// 小数点
    case dot
    /// 加减乘除
    case operation(Operation)
    /// 清除、取反、取余
    case command(Command)
}

enum CalculatorButtonBackground: String {
    case digitBackground
    case operatorBackground
    case commandBackground
}

let CalculatorButtonWidth:CGFloat = 88.0
let CalculatorButtonHeight:CGFloat = 88.0
let CalculatorButtonRowSpace:CGFloat = 8.0
let CalculatorButtonFontSize:CGFloat = 38.0

extension CalculatorButtonItem {
    var title:String {
        switch self {
        case .digit(let value): return String(value)
        case .dot: return "."
        case .operation(let operation): return operation.rawValue
        case .command(let command): return command.rawValue
        }
    }
    
    var size: CGSize {
        if case .digit(let value) = self, value == 0 {
            return CGSize(width: CalculatorButtonWidth * 2 + CalculatorButtonRowSpace, height: CalculatorButtonHeight)
        }
        return CGSize(width: CalculatorButtonWidth, height: CalculatorButtonHeight)
    }
    
    var buttonFont: CGFloat {
        CalculatorButtonFontSize
    }
    
    var buttonCornerRadius:CGFloat {
        CalculatorButtonWidth * 0.5
    }
    
    var backgroundColorName: String {
        switch self {
        case .digit, .dot: return CalculatorButtonBackground.digitBackground.rawValue
        case .operation: return CalculatorButtonBackground.operatorBackground.rawValue
        case .command: return CalculatorButtonBackground.commandBackground.rawValue
        }
    }
}

extension CalculatorButtonItem : Hashable {}
